# -*- encoding: utf-8 -*-
# parser module
############################################################################
#    Copyright (C) 2007 by Håvard Gulldahl                                 #
#    havard@gulldahl.no                                                    #
#                                                                          #
#    This program is free software; you can redistribute it and#or modify  #
#    it under the terms of the GNU General Public License as published by  #
#    the Free Software Foundation; either version 2 of the License, or     #
#    (at your option) any later version.                                   #
#                                                                          #
#    This program is distributed in the hope that it will be useful,       #
#    but WITHOUT ANY WARRANTY; without even the implied warranty of        #
#    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         #
#    GNU General Public License for more details.                          #
#                                                                          #
#    You should have received a copy of the GNU General Public License     #
#    along with this program; if not, write to the                         #
#    Free Software Foundation, Inc.,                                       #
#    59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.             #
############################################################################

import logging, re
import BeautifulSoup
import parsers

def identifiser_postjournal(journal_suppe):
    """pass"""
    logging.debug('identifiserer postjournalen')
    try:
        if journal_suppe.head.find('link',
            {'rel':'stylesheet', 'href':'/k2000/k2post.nsf/lupStyles/Standard.css'}):
            return finn_parser('k2000').parser
    except TypeError: ## ikke funnet, fortsett fingerprinting
        pass
    try:
        if journal_suppe.head.find('link',
            {'rel':'stylesheet', 'href':'/k2000-r7/k2post.nsf/lupStyles/Standard.css'}):
            return finn_parser('k2000-r7').parser
    except TypeError: ## ikke funnet, fortsett fingerprinting
        pass
    try:
        foot = journal_suppe.find('td', 'footer')
        if foot.next == u'2003 \xa9 ' and foot.a.string == u'Jupiter System Partner AS':
            return finn_parser('jupiter2003').parser
    except AttributeError:
        pass
    try:
        foot = journal_suppe.find('div', 'footer')
        if foot.next.strip() == u'2010 \xa9' and foot.a.string == u'Jupiter System Partner AS':
            return finn_parser('jupiter2010').parser
    except AttributeError:
        pass
    try:
        foot = journal_suppe.footer
        if foot.div.next.strip() == u'2013 \xa9' and foot.a.string == u'Jupiter System Partner AS':
            return finn_parser('jupiter2013').parser
    except AttributeError:
        pass
    if journal_suppe.find(text=re.compile(r"try{top.document.title = \('ePhorte ")):
        return finn_parser('ePhorte').parser
    if journal_suppe.head.find('meta', {'name':'generator', 'content':'www.CustomPublish.com'}):
        return finn_parser('customPublish').parser
    if journal_suppe.find('div', {'id':'innsynDiv', 'onkeypress':"javascript:return KlikkKnappVedReturn(event, 'sok')"}):
        return finn_parser('acos').parser
    try:
        if journal_suppe.find('div', 'MargTekst Bunntekst').a.string == u'ACOS AS':
            return finn_parser('acos').parser
    except AttributeError:
        pass
    if journal_suppe.find('a', {'class':'Bunntekst', 'href':'http://www.acos.no'}, text='ACOS AS') == u'ACOS AS':
        return finn_parser('acos').parser
    try:
        if journal_suppe.find('div', 'innsyn max-width').find('div', 'innsyn-menu'):
            return finn_parser('acos').parser
    except AttributeError:
        pass
    try:
        if journal_suppe.link['href'] == u'ePhorte_PJ.CSS':
            return finn_parser('ePhorte-gammel').parser
    except TypeError:
        pass
    try:
        if journal_suppe.find(text=re.compile('Vel?\ periode:')).parent['class'] == u'searchFieldLabel':
            return finn_parser('ePhorte-relativ').parser
    except AttributeError:
        pass
    try:
        if journal_suppe.find('td', {'class':'header','width':'100%','height':'14px'}).string.strip() == u'Postliste':
            return finn_parser('intentor').parser
    except AttributeError:
        pass
    try:
        if journal_suppe.link['href'] == u'windowscalendar.css':
            return finn_parser('dms2002').parser
    except TypeError:
        pass

    logging.error('ukjent postjournal')
    return None ## ukjent fingerprint


def finn_parser(parsernavn):
    if parsernavn in parsers.__all__:
        return smart_import('jeje.parsers.%s' % parsernavn)
    return False

def smart_import(modulnavn): ## from effbot.org
    m = __import__(modulnavn)
    for n in modulnavn.split(".")[1:]:
        m = getattr(m, n)
    return m
